package com.softa.common.sysconfig.mybatis.mapper;

import java.util.Date;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;


/**
 * 字段填充
 * @author panda
 * @since 2018年5月10日
 */
public class BootMetaObjectHandler implements MetaObjectHandler {

    protected static final String MODIFIED_DATE = "modifiedDate";
    protected static final String MODIFIED_BY = "modifiedBy";
    protected static final String CREATED_DATE = "createdDate";
    protected static final String CREATED_BY = "createdBy";
    protected static final String DELETED = "deleted";
    protected static final String VERSION = "version";

    
    @Override
    public void insertFill(MetaObject metaObject) {
        Object v = null;
        if (metaObject.hasSetter(CREATED_DATE)) {
            v = getFieldValByName(CREATED_DATE, metaObject);
            if(v == null) {
                setInsertFieldValByName(CREATED_DATE, new Date(), metaObject);
            }
        }
        if (metaObject.hasSetter(CREATED_BY)) {
            v = getFieldValByName(CREATED_BY, metaObject);
            if(v == null) {
                setInsertFieldValByName(CREATED_BY, "panda", metaObject);
            }
        }
        if (metaObject.hasSetter(MODIFIED_DATE)) {
            v = getFieldValByName(MODIFIED_DATE, metaObject);
            if(v == null) {
                setInsertFieldValByName(MODIFIED_DATE, new Date(), metaObject);
            }
        }
        if (metaObject.hasSetter(MODIFIED_BY)) {
            v = getFieldValByName(MODIFIED_BY, metaObject);
            if(v == null) {
                setInsertFieldValByName(MODIFIED_BY, "panda", metaObject);
            }
        }
        if (metaObject.hasSetter(DELETED)) {
            v = getFieldValByName(DELETED, metaObject);
            if(v == null) {
                setInsertFieldValByName(DELETED, Boolean.FALSE, metaObject);
            }
        }
        if (metaObject.hasSetter(VERSION)) {
            v = getFieldValByName(VERSION, metaObject);
            if(v == null) {
                setInsertFieldValByName(VERSION, 0L, metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object v = null;
        if (metaObject.hasSetter(MODIFIED_DATE)) {
//            v = getFieldValByName(MODIFIED_DATE, metaObject);
//            if(v == null) {
                setUpdateFieldValByName(MODIFIED_DATE, new Date(), metaObject);
//            }
        }
        if (metaObject.hasSetter(MODIFIED_BY)) {
//            v = getFieldValByName(MODIFIED_BY, metaObject);
//            if(v == null) {
                setUpdateFieldValByName(MODIFIED_BY, "panda2", metaObject);
//            }
        }
        if (metaObject.hasSetter(VERSION)) {
            v = getFieldValByName(VERSION, metaObject);
            if(v != null) {
                setUpdateFieldValByName(VERSION, v, metaObject);
            }else if(metaObject.hasGetter(VERSION)) {
                setUpdateFieldValByName(VERSION, 0L, metaObject);
            }
        }
    }

}
